package com.yoomiito.app.present.my.fragment;

import android.text.TextUtils;
import android.view.View;

import com.yoomiito.app.Constant;
import com.yoomiito.app.model.my.MyTodayOrderInfo;
import com.yoomiito.app.net.YmApi;
import com.yoomiito.app.ui.fragment.my.MyOrderFragment;
import com.yoomiito.app.utils.AllUtils;
import com.yoomiito.app.utils.NetworkUtils;
import com.yoomiito.app.utils.SpannableUtils;
import com.yoomiito.app.utils.YMToast;

import java.util.List;

import cn.droidlover.xdroidmvp.mvp.XPresent;
import cn.droidlover.xdroidmvp.net.ApiException;
import cn.droidlover.xdroidmvp.net.ApiSubscriber;
import cn.droidlover.xdroidmvp.net.XApi;

public class MyOrderFmPresenter extends XPresent<MyOrderFragment> {

    public void getTodayOrderData(int page, int type) {
        if (NetworkUtils.checkNetPermission(getV().getContext())) {
            getV().finishRefresh();
            return;
        }
        YmApi.getYmService()
                .getTodayOrder(page, type, Constant.PAGE_SIZE)
                .compose(getV().bindToLifecycle())
                .compose(XApi.<MyTodayOrderInfo>getApiTransformerData())
                .compose(XApi.<MyTodayOrderInfo>getScheduler())
                .subscribe(new ApiSubscriber<MyTodayOrderInfo>() {
                    @Override
                    public void onSuccess(MyTodayOrderInfo data) {
                        getV().setTodayOrderData(data);
                    }

                    @Override
                    public void onFail(ApiException error) {
                        YMToast.showText("error");
                    }
                });
    }


    public void getTip(View view, String orderId) {
        YmApi.getYmService()
                .getFreeGiftOrderTip(orderId)
                .compose(getV().bindToLifecycle())
                .compose(XApi.getApiTransformerData())
                .compose(XApi.getScheduler())
                .subscribe(new ApiSubscriber<List<String>>() {
                    @Override
                    public void onSuccess(List<String> t) {
                        getV().setTip(view, t);
                    }

                    public void onFail(ApiException error) {
                        getV().dismissLoading();
                        if (error!=null&&!TextUtils.isEmpty(error.getMsg())){
                            YMToast.showCenterToast(error.getMsg());
                        }
                    }
                });
    }
}
